<?php
/**
 * Created by PhpStorm.
 * User: wuchao
 * Date: 2020/4/15
 * Time: 10:48
 */

namespace app\api\model;

use \Exception;
use think\facade\Db;
use \think\response\Json;

class GoodsModel
{
    private static $_table = 'goods'; // banner表
    private static $goods_specific = 'goods_specific'; // 商品规格表
    private static $goods_reply = 'goods_reply'; // 商品评论表
//    private static $_dirver_routeTable = 'dirver_route'; // 司机路由表

    /**获取是否新品商品
     * @param array $input
     * @return Json
     */
    public static function GetGoodsNew($input)
    {
        $data = Db::table(self::$_table)->where('admin_id', $input['admin_id'])->where('is_new', 1)->where('stock', '<>',0)->where('status', 1)->field('id,name,info,image,price,ot_price,sales,stock')->limit(5)->select()->toArray();
        return $data;
    }

    /**获取是否热品商品
     * @param array $input
     * @return Json
     */
    public static function GetGoodsHot($input)
    {
        $data = Db::table(self::$_table)->where('admin_id', $input['admin_id'])->where('stock', '<>',0)->where('is_hot', 1)->where('status', 1)->field('id,name,info,image,price,ot_price,sales,stock')->limit(5)->select()->toArray();
        return $data;
    }

    /**模糊搜索商品
     * @param array $input
     * @return Json
     */
    public static function search($input)
    {
        if($input['text']){
            if ($input['is_new'] != 0 && !empty($input['priceOrder'])) {//新品且价格排序
                $data = Db::table(self::$_table)->where('status', 1)->where('is_new', $input['is_new'])->where('stock', '<>',0)->where('admin_id', $input['admin_id'])->whereLike('name', '%' . $input['text'] . '%')->page($input['page'], $input['limit'])->order('price', $input['priceOrder'])->field('name,image,cate_id,price,ot_price,is_hot,stock,sales')->select()->toArray();
                return jsonSuccess('获取成功', $data);
            } else if ($input['is_new'] != 0 && !empty($input['salesOrder'])) {//新品且销量排序
                $data = Db::table(self::$_table)->where('status', 1)->where('is_new', $input['is_new'])->where('stock', '<>',0)->where('admin_id', $input['admin_id'])->whereLike('name', '%' . $input['text'] . '%')->page($input['page'], $input['limit'])->order('sales', $input['salesOrder'])->field('name,image,cate_id,price,ot_price,is_hot,stock,sales')->select()->toArray();
                return jsonSuccess('获取成功', $data);
            } else if ($input['is_hot'] != 0 && !empty($input['salesOrder'])) {//热卖且销量排序
                $data = Db::table(self::$_table)->where('status', 1)->where('is_hot', $input['is_new'])->where('stock', '<>',0)->where('admin_id', $input['admin_id'])->whereLike('name', '%' . $input['text'] . '%')->page($input['page'], $input['limit'])->order('sales', $input['salesOrder'])->field('name,image,cate_id,price,ot_price,is_hot,stock,sales')->select()->toArray();
                return jsonSuccess('获取成功', $data);
            } else if ($input['is_hot'] != 0 && !empty($input['priceOrder'])) {//热卖且价格排序
                $data = Db::table(self::$_table)->where('status', 1)->where('is_hot', $input['is_hot'])->where('stock', '<>',0)->where('admin_id', $input['admin_id'])->whereLike('name', '%' . $input['text'] . '%')->page($input['page'], $input['limit'])->order('price', $input['priceOrder'])->field('name,image,cate_id,price,ot_price,is_hot,stock,sales')->select()->toArray();
                return jsonSuccess('获取成功', $data);
            } else if (!empty($input['priceOrder'])) {//价格排序
                $data = Db::table(self::$_table)->where('status', 1)->where('admin_id', $input['admin_id'])->where('stock', '<>',0)->whereLike('name', '%' . $input['text'] . '%')->page($input['page'], $input['limit'])->order('price', $input['priceOrder'])->field('name,image,bar_code,cate_id,price,ot_price,is_hot,stock,sales')->select()->toArray();
                return jsonSuccess('获取成功', $data);
            }else if (!empty($input['salesOrder'])) {//销量排序
                    $data = Db::table(self::$_table)->where('status', 1)->where('admin_id', $input['admin_id'])->where('stock', '<>',0)->whereLike('name', '%' . $input['text'] . '%')->page($input['page'], $input['limit'])->order('sales', $input['salesOrder'])->field('name,image,cate_id,price,ot_price,is_hot,stock,sales')->select()->toArray();
                    return jsonSuccess('获取成功', $data);
            } else {
                $data = Db::table(self::$_table)->where('status', 1)->where('admin_id', $input['admin_id'])->where('stock', '<>',0)->whereLike('name', '%' . $input['text'] . '%')->page($input['page'], $input['limit'])->select()->toArray();
                return jsonSuccess('获取成功', $data);
            }
        }else{
            return jsonFail('搜索不能为空');
        }

    }
    /**商品详情
     * @param array $input
     * @return Json
     */
    public static function detail($input)
    {
        $field = 'center_id,coop_id,create_time,cate_id,description,id,image,status,is_hot,ot_price,price,sort,stock,name,sales';
        $storeInfo = Db::table(self::$_table)->where('status', 1)->where('id', $input['goods_id'])->field($field)->find();
        if (empty($storeInfo)) return jsonFail('暂无商品');
        //替换windows服务器下正反斜杠问题导致图片无法显示
        $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
            return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
        }, $storeInfo['description']);
        $data['productInfo'] = $storeInfo;
        $data['productAttr'] = self::getProductAttrDetail($input['goods_id'], $input['admin_id']);//TODO 获取商品属性
        $data['productReply'] = self::getProductAttrReply($input['goods_id'], $input['admin_id']);//TODO 获取商品评论
        return jsonSuccess('获取成功', $data);
    }

    /**
     * 获取商品属性数据
     * @param $productId
     * @return array
     */
    public static function getProductAttrReply($productId, $admin_id)
    {
        $field = 'product_score,service_score,comment,pics';
        $reply = Db::table(self::$goods_reply)->where('status', 1)->where('product_id', $productId)->field($field)->select()->toArray() ?: [];
        return $reply;
    }
    /**
     * 获取商品属性数据
     * @param $productId
     * @return array
     */
    public static function getProductAttrDetail($productId, $admin_id)
    {
        $field = 'id,name,goods_id,pic,price,center_stock as stock';
        $attrDetail = Db::table(self::$goods_specific)->where('status', 1)->where('admin_id', $admin_id)->where('goods_id', $productId)->field($field)->select();
        return $attrDetail;
    }


    //curl进行Https Get访问链接
    public
    static function curl_url($url)
    {
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $output = curl_exec($ch); //执行并获取HTML文档内容
        $data = json_decode($output);
        //释放curl句柄
        curl_close($ch);
        return $data;
    }
}